from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time

#NBA官网
url='https://www.nba.com/stats/players/traditional/?sort=AGE&dir=-1'

driver=webdriver.Chrome("chromedriver.exe")
driver.implicitly_wait(10)
driver.get(url)#获取页面信息

#获取每一页的表格数据
page_num=1
pages_all=True
while pages_all:
    soup=BeautifulSoup(driver.page_source,'lxml')
    table=soup.select_one("div.nba-stat-table__overflow>table")
    # print(table)
    df=pd.read_html(str(table))
    df[0].to_csv("all_players"+str(page_num)+".csv")
    # driver.quit()
    #定义选择下一页的数据
    try:
        next_page=driver.find_element_by_xpath('/html/body/main/div/div/div[2]/div/div/nba-stat-table/div[3]/div/div/a[2]')
        next_page.click()
        time.sleep(5)
        if page_num<5:
            page_num+=1
        else:
            pages_all=False
    except Exception:
        pages_all=False





